package com.heima.search.controller.v1;

import org.apache.lucene.index.Term;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-09-22 13:41
 */
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @GetMapping("/aggregate")
    public List<Map> list() throws IOException {

        SearchRequest request = new SearchRequest("app_info_article");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SearchRequest source = request.source(searchSourceBuilder);

        searchSourceBuilder.aggregation(AggregationBuilders.terms("authorAgg").field("authorId").size(10));

        SearchResponse search = restHighLevelClient.search(source, RequestOptions.DEFAULT);
        Aggregations aggregations = search.getAggregations();
        Terms authorAgg = aggregations.get("authorAgg");
        List<? extends Terms.Bucket> buckets = authorAgg.getBuckets();
        return buckets.stream().map(x -> {
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put(x.getKey(),x.getDocCount());
            return objectObjectHashMap;
        }).collect(Collectors.toList());
    }

    @GetMapping("/suggest")
    public List<String> suggest(String name) throws IOException{
        SearchRequest request = new SearchRequest("leadnews_suggest");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SearchRequest source = request.source(searchSourceBuilder);

        searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion("title_suggest",SuggestBuilders.completionSuggestion("suggestion").
                prefix(name).skipDuplicates(true).size(10)));

        SearchResponse search = restHighLevelClient.search(source, RequestOptions.DEFAULT);

        CompletionSuggestion suggestion = search.getSuggest().getSuggestion("title_suggest");

        List<String> collect = suggestion.getOptions().stream().map(Suggest.Suggestion.Entry.Option::getText).map(Text::string).collect(Collectors.toList());
        return collect;
    }
}
